package wqh.dao.impl;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import wqh.dao.WaterChannelAlarmDao;
import wqh.domain.WaterChannelAlarm;

import java.util.List;

/**
 * Created by wqh on 2017/1/19.
 */
@Repository("waterChannelAlarmDao")
public class WaterChannelAlarmDaoImpl extends DaoImpl<WaterChannelAlarm> implements WaterChannelAlarmDao {

    @Override
    public void deleteByDeviceIdAndChannelNumber(Long device_id,Integer channel_number){
        Session session = this.getSessionFactory().getCurrentSession();
        String hql = "DELETE FROM WaterChannelAlarm w WHERE  w.device_id = :device_id AND w.channel_number = :channel_number";
        Query query = session.createQuery(hql);
        query.setParameter("device_id",device_id);
        query.setParameter("channel_number",channel_number);
        query.executeUpdate();
    }

    @Override
    public List<WaterChannelAlarm> findByDeviceIdAndChannelNumber(Long device_id,Integer channel_number) {
        Session session = this.getSessionFactory().getCurrentSession();
        String hql = "FROM WaterChannelAlarm w WHERE  w.device_id = :device_id AND w.channel_number = :channel_number";
        Query query = session.createQuery(hql);
        query.setParameter("device_id",device_id);
        query.setParameter("channel_number",channel_number);
        return query.list();
    }

    @Override
    public Long countByTimeRange(Integer start, Integer end) {
        Session session = this.getSessionFactory().getCurrentSession();
        String hql = "SELECT COUNT(*) FROM WaterChannelAlarm w WHERE  w.alarm_time BETWEEN :start AND :end";
        Query query = session.createQuery(hql);
        query.setParameter("start",start);
        query.setParameter("end",end);
        return  query.uniqueResult() == null ? 0 : (Long) query.uniqueResult();
    }
}
